<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");
require_once($root."functions/common/common.php");
require_once($root."functions/general_function.php");
require_once($root."functions/downline/downline_function.php");
require_once($root."functions/comm/comm_function.php");
require_once($root."initialize.php");

if(1){//ndate("d")=="01" && ndate("H")=="00"){
	if(($db_msg=cron_initialize()) !== true){
		$errmsg="Error starting group sales calculation due to database error.<br />\n<br />\n$db_msg";
		$a_sub="$vars[title] - Group Sales Calculation Failed";
		$a_msg=
		"<p>Dear admin,</p>
		$errmsg";
		email_admin($a_sub, $a_msg, 'e');
	}else{
		$db=$vars["db"];
		$curday=ndate('d');
		$curmonth=ndate('n');
		$time_start = mktime(0, 0, 0 , $curmonth-1, 1, ndate("Y"));
		$time_end = mktime(0, 0, 0 , $curmonth, 0, ndate("Y"));
		$year = date('Y', $time_start);
		$month = date('n', $time_start);
		$day_start = date('j', $time_start);
		$day_end = date('j', $time_end);

		//####################ps pgs####################
		$comm_title = "Group Sales Calculations";
		$comm_code = "g";
		$comm_date=date('Y-m-d H:i:s', $time_start);
		$comm_date_d=date('M-Y', $time_start);

		//executed = check cron_table if exist with type = placement, day, month, year
		if(!($r=mysql_query("select count(*) from $db->cron where type='$comm_code' and year='$year' and month='$month'"))){
			$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date_d] due to database error.<br />\n<br />\n".mysql_error();
		}else{
			if(($executed[$comm_code] = @mysql_result($r, 0)) === false){
				$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date_d] due to database error.<br />\n<br />\n".mysql_error();
			}
		}

		if(!$errmsg[$comm_code]){
			if (!$executed[$comm_code]){
				//insert into cron_table type = placement, started = y, year, month, day, cron start date = datetime
				$sql = "insert into $db->cron (type, started, completed, success, year, month) values ('$comm_code', '".ndate($vars["system_date_format"])."', 'n', 'n', '$year', '$month')";
				if(!mysql_query($sql)){
					$errmsg[$comm_code].="Error starting $comm_title calculation [$comm_date_d] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				}else{
					//cron_id = last insert id
					$cron_id = mysql_insert_id();
					$result = calc_pspgs($comm_date);
					$comm_errmsg_d = '';
					if($result['status'] === true){
						$success = 'y';
						$executed[$comm_code] = true;
					}else{
						$errmsg[$comm_code] = "$comm_title calculation [$comm_date_d] completed with errors:<br />\n<br />\n".$comm_errmsg;
						$success = 'n';
					}
					if($result['errmsg']){
						$comm_errmsg = $result['errmsg'];
						$comm_errmsg_d = mysql_real_escape_string($result['errmsg']);
					}
					$sql = "update $db->cron set completed='y', success='$success', ended='".ndate($vars["system_date_format"])."', message='$comm_errmsg_d' where id='$cron_id'";
					if(!mysql_query($sql)){
						$errmsg[$comm_code]="Error completing the $comm_title calculation [$comm_date_d] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n".($errmsg[$comm_code]? "<br />\n".$errmsg[$comm_code] : "");
					}
				}
			}
		}
		if ($errmsg[$comm_code]){
			$a_sub="$vars[title] - $comm_title Calculation [$comm_date_d] with Error";
			$a_msg=
			"<p>Dear admin,</p>
			".$errmsg[$comm_code];
			email_admin($a_sub, $a_msg, 'e');
		}
		
		//####################bonus summary####################
		$comm_title = "Bonus Summary Calculations";
		$comm_code = "h";
		$comm_date=date('Y-m-d H:i:s', $time_start);
		$comm_date_d=date('M-Y', $time_start);

		//executed = check cron_table if exist with type = placement, day, month, year
		if(!($r=mysql_query("select count(*) from $db->cron where type='$comm_code' and year='$year' and month='$month'"))){
			$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date_d] due to database error.<br />\n<br />\n".mysql_error();
		}else{
			if(($executed[$comm_code] = @mysql_result($r, 0)) === false){
				$errmsg[$comm_code]="Error starting $comm_title Calculation [$comm_date_d] due to database error.<br />\n<br />\n".mysql_error();
			}
		}

		if(!$errmsg[$comm_code]){
			if (!$executed[$comm_code]){
				//insert into cron_table type = placement, started = y, year, month, day, cron start date = datetime
				$sql = "insert into $db->cron (type, started, completed, success, year, month) values ('$comm_code', '".ndate($vars["system_date_format"])."', 'n', 'n', '$year', '$month')";
				if(!mysql_query($sql)){
					$errmsg[$comm_code].="Error starting $comm_title calculation [$comm_date_d] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n";
				}else{
					//cron_id = last insert id
					$cron_id = mysql_insert_id();
					$result = calc_comm_summary($comm_date);
					$comm_errmsg_d = '';
					if($result['status'] === true){
						$success = 'y';
						$executed[$comm_code] = true;
					}else{
						$errmsg[$comm_code] = "$comm_title calculation [$comm_date_d] completed with errors:<br />\n<br />\n".$comm_errmsg;
						$success = 'n';
					}
					if($result['errmsg']){
						$comm_errmsg = $result['errmsg'];
						$comm_errmsg_d = mysql_real_escape_string($result['errmsg']);
					}
					$sql = "update $db->cron set completed='y', success='$success', ended='".ndate($vars["system_date_format"])."', message='$comm_errmsg_d' where id='$cron_id'";
					if(!mysql_query($sql)){
						$errmsg[$comm_code]="Error completing the $comm_title calculation [$comm_date_d] due to database error.<br />\n<br />\nSQL: $sql<br />\n<br />\nError: ".mysql_error()."<br />\n".($errmsg[$comm_code]? "<br />\n".$errmsg[$comm_code] : "");
					}
				}
			}
		}
		if ($errmsg[$comm_code]){
			$a_sub="$vars[title] - $comm_title Calculation [$comm_date_d] with Error";
			$a_msg=
			"<p>Dear admin,</p>
			".$errmsg[$comm_code];
			email_admin($a_sub, $a_msg, 'e');
		}
	}
}
?>